package it.univpm.diiga.memorysim.actions;

import it.univpm.diiga.memorysim.data.DataPlugin;
import it.univpm.diiga.memorysim.views.OutputSimulation;
import it.univpm.diiga.memorysim.views.OutputSimulationHDD;
import it.univpm.diiga.memorysim.views.OutputTable;
import it.univpm.diiga.memorysim.views.OutputTableFile;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;


public class StartSim implements IWorkbenchWindowActionDelegate {
	public static IWorkbenchWindow window;
	public static boolean codaprocessi=false;
	/**
	 * The constructor.
	 */
	public StartSim() {
	}


	public void run(IAction action) {
		DataPlugin.initSimulation();
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		try {
			OpenBytype();
		} catch (PartInitException e1) {
			MessageDialog.openError(window.getShell(), "Error", "Error opening view:" + e1.getMessage());
		}
	}

	private void OpenBytype() throws PartInitException{
		if(DataPlugin.SimType==1){
			 if(!DataPlugin.ControlMem()) MessageDialog.openError(window.getShell(), "Error", "La simulazione non richiede nessun processo");
			 window.getActivePage().showView(OutputSimulation.ID, "proc mem", IWorkbenchPage.VIEW_ACTIVATE);
			 window.getActivePage().showView(OutputTable.ID, "proc tab", IWorkbenchPage.VIEW_ACTIVATE);
		}
		if(DataPlugin.SimType==2){
			if(!DataPlugin.ControlDisk()) MessageDialog.openError(window.getShell(), "Error", "La simulazione non richiede nessun file");
			 window.getActivePage().showView(OutputSimulationHDD.ID, "file mem0", IWorkbenchPage.VIEW_ACTIVATE);
			 window.getActivePage().showView(OutputTableFile.ID, "file tab", IWorkbenchPage.VIEW_ACTIVATE);
		}
		if(DataPlugin.SimType==3){
			if(!DataPlugin.ControlMem()) MessageDialog.openError(window.getShell(), "Error", "La simulazione non richiede nessun processo");
			if(!DataPlugin.ControlDisk()) MessageDialog.openError(window.getShell(), "Error", "La simulazione non richiede nessun file");
			 window.getActivePage().showView(OutputSimulation.ID, "proc mem", IWorkbenchPage.VIEW_ACTIVATE);
			 window.getActivePage().showView(OutputTable.ID, "proc tab", IWorkbenchPage.VIEW_ACTIVATE);
			 window.getActivePage().showView(OutputSimulationHDD.ID, "file mem0", IWorkbenchPage.VIEW_ACTIVATE);
			 window.getActivePage().showView(OutputTableFile.ID, "file tab", IWorkbenchPage.VIEW_ACTIVATE);
		}
		if(DataPlugin.SimType==0){
			MessageDialog.openError(window.getShell(), "Error", "Nessun Tipo di Simulazione Selezionata");
		}
	}
	
	public void selectionChanged(IAction action, ISelection selection) {
	}


	public void dispose() {
	}


	@SuppressWarnings("static-access")
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}